package com.bstek.uflo.console.view;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.uflo.command.Command;
import com.bstek.uflo.command.CommandService;
import com.bstek.uflo.command.impl.GetAllCalendarDefCommand;
import com.bstek.uflo.command.impl.GetCalendarDateCommand;
import com.bstek.uflo.console.command.DeleteCalendarDateCommand;
import com.bstek.uflo.console.command.DeleteCalendarDefCommand;
import com.bstek.uflo.console.command.SaveCalendarDateCommand;
import com.bstek.uflo.console.command.SaveCalendarDefCommand;
import com.bstek.uflo.model.calendar.CalendarDate;
import com.bstek.uflo.model.calendar.CalendarDef;
import com.bstek.uflo.utils.IDGenerator;

/**
 * 
 * @author Jake.Wang@bstek.com
 * @since Sep 23, 2013
 * 
 */
@Component("uflo.calendarMaintain")
public class CalendarMaintain {
	@Autowired
	@Qualifier("uflo.commandService")
	private CommandService commandService;

	@DataProvider
	public Collection<CalendarDef> loadCalendarDef() {
		return execute(new GetAllCalendarDefCommand());
	}

	@DataProvider
	public Collection<CalendarDate> loadCalendarDate(Long calendarId) {
		return execute(new GetCalendarDateCommand(calendarId));
	}

	@DataResolver
	public void saveCalendar(List<CalendarDef> calendars) {
		for (CalendarDef def : calendars) {
			EntityState state = EntityUtils.getState(def);
			if (state.equals(EntityState.NEW)) {
				def.setId(IDGenerator.getInstance().nextId());
				execute(new SaveCalendarDefCommand(def));
			}
			if (state.equals(EntityState.MODIFIED)) {
				execute(new SaveCalendarDefCommand(def));
			}
			if (state.equals(EntityState.DELETED)) {
				execute(new DeleteCalendarDefCommand(def));
			} else if (def.getCalendarDates() != null) {
				this.saveCalendarDates(def.getCalendarDates());
			}
		}
	}

	private void saveCalendarDates(Collection<CalendarDate> dates) {
		for (CalendarDate date : dates) {
			EntityState state = EntityUtils.getState(date);
			if (state.equals(EntityState.NEW)) {
				date.setId(IDGenerator.getInstance().nextId());
				execute(new SaveCalendarDateCommand(date));
			}
			if (state.equals(EntityState.MODIFIED)) {
				execute(new SaveCalendarDateCommand(date));
			}
			if (state.equals(EntityState.DELETED)) {
				execute(new DeleteCalendarDateCommand(date));
			}
		}
	}

	private <T> T execute(Command<T> command) {
		return commandService.executeCommand(command);
	}
}
